﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TagCloudSoma
{
    public class TagCloud
    {
        InputCleaner _iP;
        WordExcluder _wE;
        Dictionary<string,int> _dic;
        WebController _wC;

        #region constructor
            public TagCloud() 
            {
                _iP = new InputCleaner();
                _wE = new WordExcluder();
                _dic = new Dictionary<string,int>();
                _wC = new WebController();
            }
        #endregion

        #region Private Method
            private void counting(string s)
        {
            _dic.Clear();
            string[] arrText;
            string w;

            arrText = s.Trim().Split(' ');

            Dictionary<string, int> dicAux = new Dictionary<string, int>();

            foreach (string word in arrText)
            {
                w = word.Trim().ToLower();
                if ((!_wE.Contains(w)) && (w.Length > 0))
                {
                    if (!dicAux.ContainsKey(w))
                    {
                        dicAux.Add(w, 1);
                    }
                    else
                    {
                        dicAux[w] = dicAux[w] + 1;
                    }
                }
            }


            var sortedDict = (from entry in dicAux orderby entry.Value descending select entry);

            foreach (KeyValuePair<string, int> entry in sortedDict)
            {
                _dic.Add(entry.Key, entry.Value);
            }
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// Clean up the string s 
        /// </summary>
            public string cleaner(string s)
            {
                _iP.Inpunt = s;
                return _iP.cleaner();
            }
            
        /// <summary>
        /// Creates a sorted dictionary of the words most frequently used
        /// </summary>
            public Dictionary<string,int> wordCounter(string s, int num)
            {
                Dictionary<string,int> trunkSL;
                this.counting(s);
               

                if (_dic.Count > 0 && _dic.Count>num)
                {
                    trunkSL = new Dictionary<string,int>();

                    for( int i=0; i<num; i++)
                    {
                        KeyValuePair<string,int> t = _dic.ElementAt(i);
                        trunkSL.Add(t.Key, t.Value);
                    }
                    return trunkSL;
                }
                else return _dic;
            }
            
        /// <summary>
        ///  Strips all html tags
        /// </summary>
            public string stripHTMLTag(string source)
            {    
                return _wC.StripHTMLTag(source);
            }
            
        /// <summary>
        /// Gets the html source of a website
        /// </summary>
            public string GetSource(string uri)
            {
                return _wC.GetSource(uri);
            }

        #endregion
    }
}
